import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/default_style.dart';
import 'package:flutter_pickers/style/picker_style.dart';

class SelectBox extends StatefulWidget {
  final double height;
  final String initialValue;
  final List<String> options;
  final String label;
  final ValueChanged<String> onChanged;

  const SelectBox({
    super.key,
    required this.initialValue,
    required this.options,
    required this.onChanged,
    this.height = 36,
    required this.label,
  });

  @override
  State<SelectBox> createState() => _SelectBoxState();
}

class _SelectBoxState extends State<SelectBox> {
  late String _selectedValue;

  @override
  void initState() {
    super.initState();
    _selectedValue = widget.initialValue;
  }
  @override
  void didUpdateWidget(oldWidget) {
    super.didUpdateWidget(oldWidget);
    _selectedValue = widget.initialValue;
  }
  //利用自带的库的选择器
  /*  void _showSelector() {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
          height: 250,
          child: CupertinoPicker(
            backgroundColor: Colors.white,
            itemExtent: 30,
            children:
                widget.options.map((option) {
                  return Text(option);
                }).toList(),
            onSelectedItemChanged: (index) {
              setState(() {
                _selectedValue = widget.options[index];
              });
              widget.onChanged(_selectedValue);
            },
          ),
        );
      },
    );
  }*/

  void _showSelector() {
    Pickers.showSinglePicker(
      context,
      data: ['不选择', ...widget.options],
      selectData: _selectedValue,
      pickerStyle: PickerStyle(
        commitButton: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.only(left: 12, right: 22),
          child: Text(
            '确认',
            style: TextStyle(color: Colors.blue, fontSize: 16.0),
          ),
        ),
      ),
      onConfirm: (picker, index) {
        if (picker == '不选择') {
          setState(() {
            _selectedValue = widget.label;
          });
        } else {
          setState(() {
            _selectedValue = picker;
          });
        }
        widget.onChanged(_selectedValue);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: _showSelector,
        child: Container(
          height: widget.height,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
            borderRadius: BorderRadius.circular(4),
          ),
          padding: const EdgeInsets.only(left:6),
          alignment: Alignment.centerLeft,
          child: Text(
            _selectedValue,
            style: const TextStyle(fontSize: 16, color: Colors.grey),
          ),
        ),
      ),
    );
  }
}
